﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Mail;

namespace T2.WebSecurity.Business.Business
{
    public class EmailHelper
    {
        private MailMessage Message { get; set; }

        public int Port { get; set; }
        public string Host { get; set; }

        public EmailHelper()
        {
            Message = new MailMessage();
            Message.IsBodyHtml = true;
            Host = String.Empty;
        }

        public EmailHelper(string toAddress,string toName, string fromAddress,string fromName, string subject, string body)
        : this()
        {
            this.AddToAddress(toAddress, toName);
            this.FromAddress(fromAddress, fromName);
            Message.Subject = subject;
            Message.Body = body;
        }

        public string Subject
        {
            get { return Message.Subject; }
            set { Message.Subject = value; }
        }

        public string Body
        {
            get { return Message.Body; }
            set { Message.Body = value; }
        }

        // return false when address is invalid email
        public bool FromAddress(string address, string name)
        {
            MailAddress mAddress;
            try
            {
                mAddress = new MailAddress(address, name);
                Message.From = mAddress;
                return true;
            }
            catch (FormatException ex)
            {
                return false;
            }
        }

        // return false when address is invalid email
        public bool AddToAddress(string address, string name)
        {
            MailAddress mAddress;
            try
            {
                mAddress = new MailAddress(address, name);
                Message.To.Add(mAddress);
                return true;
            }
            catch (FormatException ex)
            {
                return false;
            }
        }

        // return false when address is invalid email
        public bool AddCCAddress(string address, string name)
        {
            MailAddress mAddress;
            try
            {
                mAddress = new MailAddress(address, name);
                Message.CC.Add(mAddress);
                return true;
            }
            catch (FormatException ex)
            {
                return false;
            }
        }

        public void Send()
        {
            SmtpClient smtp = new SmtpClient();
            
            if (Port > 0)
                smtp.Port = Port;

            if (!Host.Equals(""))
                smtp.Host = Host;
            
            try
            {
                smtp.Send(Message);
            }
            catch (Exception ex)
            {
                
                throw new Exception("Email has not been sent"+ex.Message);
            }
        }
    }
}
